﻿
@{
    ViewData["Title"] = "用户角色授予";
}

@section Scripts
    {

    <script type="text/javascript">
        var selfLayer = null, page = 1, active = null;
        layui.use(['layer', 'table', 'laydate','treeSelect', 'form'], function () {
            var form = layui.form ,treeSelect = layui.treeSelect;

                 //请选择权限分类
                axios({
                    url: "/Authorization/GetRoleAll",
                    method: 'get',
                })
                    .then(function (response) {
                        console.log(response.data);
                        if (response.data.code == 200 ) {
                            var options = '<option value="">请选择分配权限</option>';
                            if (response.data.code == 200) {
                                for (var i = 0; i < response.data.result.data.length; i++) {
                                    options += '<option value="' + response.data.result.data[i].id + '">' + response.data.result.data[i].roleName + '</option>';
                                }
                            }
                            $("#RoleId").html(options);
                            layui.use('form', function () {
                                var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                                form.render();
                            });
                        } else {
                            console.log(response.data.msg);
                        }
                    })


            //地区
              treeSelect.render({
                // 选择器
                elem: '#varProjectName',
                // 数据
                 data: '/MarketPrice/GetAreaTreeAll?name=' + localStorage.getItem("X-AreCode"),
                // 请求头
                //  headers: { 'Authorization': 'Bearer ' + localStorage.getItem("X-TOKEN") },
                // 异步加载方式：get/post，默认get
                type: 'get',
                // 占位符
                placeholder: '地区',
                // 是否开启搜索功能：true/false，默认false
                search: true,
                // 一些可定制的样式
                style: {
                    folder: {
                        enable: true
                    },
                    line: {
                        enable: true
                    }
                },
                // 点击回调
                click: function (d) {
                    console.log(d);
                },
                // 加载完成后的回调函数
                success: function (d) {
                    console.log(d);
                    //                选中节点，根据id筛选
                    treeSelect.checkNode('varProjectName', 3);

                    // console.log($('#varProjectName').val());

                    //                获取zTree对象，可以调用zTree方法
                    var treeObj = treeSelect.zTree('varProjectName');
                    console.log(treeObj);

                    //                刷新树结构
                    treeSelect.refresh('varProjectName');
                }
            });


            var item = '@ViewData["Id"]';
            console.log(item);

               form.on('submit(submit)', function (data) {
                   var IsAttest = $("#RoleId option:selected").val();
                   var RoleAreCode = $("#varProjectName").val();
                   //console.log(IsAttest);
                   //console.log(data.field.Remarks);

                 axios.get('/Authorization/AddUserRole', {
                    params: {
                         roleId: IsAttest,
                         remarks: data.field.Remarks,
                         userId: item,
                         roleAreCode : RoleAreCode
                     }
                }).then(function (response) {
                    console.log(response);
                    if (response.data.Code == 200) {
                         javascript: history.go(-1);
                    } else {
                        alert(response.msg)
                    }
                }).catch(function (error) {
                    console.log(error);
                });
            });
        });
    </script>
}

<form class="layui-form layui-form-pane">
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-inline">
            <select lay-search="" id="RoleId" name="RoleId" autocomplete="off" lay-verify="required" onchange='btnChange();'>
            </select>
        </div>
    </div>
    <div class="layui-inline" id="AreaId">
        <label class="layui-form-label">地区：</label>
        <div class="layui-input-inline">
            <input type="text" id="varProjectName" lay-filter="tree" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea id="Remarks" name="Remarks" style="width:545px;height:100px"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input class="layui-btn" type="button" style="width: 100px" lay-submit lay-filter="submit" value="立即提交" />
            <button type="reset" style="width: 100px" class="layui-btn layui-btn-primary">重置</button>
            <a style="width: 100px" class="layui-btn layui-btn-primary backIframe" asp-action="List">返回</a>
        </div>
    </div>
</form>


